<?php
class User extends EMongoDocument
{
    static $cacheUsers = array();
    
    public $fullname;
    public $number;
    public $password;
    public $area_id;
    public $email;
    
    /**
     * the user status
     * if status=0 then the user is disable.
     * if status=1 then the user is available
     * @var int
     */
    public $status = self::STATUS_DISABLE;

    const STATUS_AVAILABLE = 1;
    const STATUS_DISABLE = 0;
    
    public function getCollectionName()
    {
        return 'user';
    }
    
    /**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function embeddedDocuments()
    {
        return array(
            'profile'=>'UserProfile',
        );
    }
    
    public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('fullname, number, password, email, area_id', 'required'),
            array('fullname', 'common.extensions.YiiMongoDbSuite.extra.EMongoUniqueValidator'),
            array('number', 'common.extensions.YiiMongoDbSuite.extra.EMongoUniqueValidator'),
            array('email', 'email'),
            array('ctime, mtime', 'safe'),
		);
    }
    
    public function beforeSave()
    {
        if(parent::beforeSave())
        {
            if($this->getIsNewRecord())
            {
                $this->password = self::encryptPassword($this->password);
            }
            
            return true;
        }
        else
            return false;
    }
    
    static public function getByNumber($number)
    {
        if( ! isset($number{0}))
            return false;
        
        $criteria = new EMongoCriteria;
        $criteria->setConditions(array(
            'number'=> (string)$number,
        ));
        
        return User::model()->find($criteria);
    }
    
    /**
     * use for auto login after create user
     * @param type $duration
     * @return type
     */
    public function login($duration=0)
    {
        $identity = UserIdentity::getByUser($this);
        return Yii::app()->user->login($identity, $duration);
    }

    public function logout()
    {
        Yii::app()->user->logout();
    }
    
    public function getUser($id='')
    {
        if(empty($id))
        {
            $webUser = Yii::app()->user;
            if( ! empty($webUser) && ! $webUser->getIsGuest())
                $id = $webUser->getId();
        }

        if(empty($id) || ! is_string($id))
            return false;

        $user = isset(self::$cacheUsers[$id]) ? self::$cacheUsers[$id] : false;
        if(empty($user) && $user = $this->getById($id))
            return self::$cacheUsers[$id] = $user;
        else
            return $user;
    }
    
    /**
     * 
     */
    public function getUserId()
    {
        $user = $this->getUser();
        if($user instanceof User)
            return $user->getId();
        
        return false;
    }
    
    /**
     * 
     * @return array()
     */
    public function getAreaOptions()
    {
        $options = array();
        $areas = Area::model()->getAll();
        if( ! empty($areas) && is_array($areas))
        {
            foreach($areas as $area)
            {
                if($area instanceof Area)
                    $options = array_merge($options, array($area->getId()=>$area->getName()));
            }
        }
        return $options;
    }
    
    static public function encryptPassword($password)
    {
        return md5($password);
    }
    
    public function getDisplayName()
    {
        return $this->fullname;
    }
}
